package com.hxz.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxz.entity.User;
import com.hxz.interceptor.LoginInterceptor;
import com.hxz.service.impl.UserServiceImpl;
import com.hxz.util.Md5Util;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 用户controller,登录校验，退出登录等功能
 * @author hxz
 * @description TODO
 * @data 2019年12月5日 上午9:31:44
 */
@Component
@Controller
@RequestMapping("/admin")
@Api(description = "用户controller,登录校验，退出登录等功能") // 注解：应用在具体的 controller 上面
public class AdminLoginController {
	@Autowired
	private UserServiceImpl userServiceImpl;


	/**
	 * 登录页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	@ApiOperation("登录页面")
	public String login() {

		return "admin/login";

	}

	/**
	 * 登录校验MD5校验
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 */
	@RequestMapping("/checklogin")
	@ApiOperation("登录校验") // 注解：应用在 controller 里面的方法上，用于swagger
	@ResponseBody
	public Map<String, Object> checklogin(@Param("username") String username, @Param("password") String password,
			HttpSession session) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String pwd = Md5Util.string2MD5(password);
		Integer statuscode = userServiceImpl.checklogin(username, pwd);
		if (statuscode > 0) {
			User user = new User();
			user.setPassword(password);
			session.setAttribute("USER_SESSION", user);
			session.setMaxInactiveInterval(180);// session失效时间为三分，即180s
			map.put("code", 1);
			return map;// redirect
		} else {
			map.put("code", 0);
			return map;
		}

	};



	/**
	 * 登出
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "logout")
	public String logout() throws Exception {

		return "admin/login";
	}

	/**
	 * 编辑文章
	 */
	@RequestMapping("article_edit")
	public String edit_article() {

		return "/admin/article_edit";
	}
}
